import math

a=2#每格暂定的px值，另一个该值在polygon中
length = 500
#计算得到每个方格代表多少米，参数为用户输入的图片可代表多长的长度（ 缩放比例* 图长）      和前端画布大小，输出每个方法表示的米数
def unit(meter,width = 900):

    return meter/width*a



#dict按照value值排序
def sort_by_value(d):
    items=d.items()
    backitems=[[v[1],v[0]] for v in items]
    backitems.sort()
    return [ backitems[i][1] for i in range(0,len(backitems))]


#args：  meter要转换的米数
#return ： 向上取整的一个方格数
def trans(meter,):
    return math.ceil(meter)



#对于二维矩阵执行退线操作("暂时规定被退线的记为9")参数为米数
#args：二维数组

def retreat(array,d):#左方退线

    list=sort_by_value(d)
    for x in range(4):
        for i in range(length):
             num=0
             if list[x] == "dis_w":
                for j in range(length):#左方退线
                    if num <  d.get("dis_w"):
                        # if array[i][j] == 9:
                        #     break
                        if array[i][j] == 1:
                            num+=1
                            array[i][j]=9
                    else:
                        break


        for i in range(length):#右方退线
            num = 0
            if list[x] == "dis_e":
                for j in range(length):
                    if num < d.get("dis_e"):
                        # if array[i][length-j-1] == 9:
                        #     break
                        if array[i][length-j-1] == 1:
                            num += 1
                            array[i][length-j-1] = 9
                    else:
                        break


        for m in range(length):#上方退线
            num = 0
            if list[x] == "dis_n":
                for n in range(length):
                    if num < d.get("dis_n"):
                        # if array[n][m] == 9:
                        #     break
                        if array[n][m] == 1:
                            num += 1
                            array[n][m] = 9
                    else:
                        break
        for m in range(length): #下方退线
            num = 0
            if list[x] == "dis_s":
                for n in range(length):
                    if num < d.get("dis_s"):
                        # if array[length-n-1][m] == 9:
                        #     break
                        if array[length-n-1][m] == 1:
                            num += 1
                            array[length-n-1][m] = 9
                    else:
                        break


    return array


def show(array,length):
    for i in range(length):
        for j in range(length):
            print(array[i][j],end=" ")
        print(" ")


#接收输入限制条件的类
class Condi:
    def __init__(self,form):
        self.rate = form.get("rate")
        self.density = form.get("density")
        self.sun = form.get("sun ")
        self.limh = form.get("limh")
        self.retreat = form.get("retreat")
        self.gate_n = form.get("gate[n]")
        self.gate_s = form.get("gate[s]")
        self.gate_w = form.get("gate[w]")
        self.gate_e = form.get("gate[e]")
        if form.get("num1"):
            self.room1 = form.get("room1")
            self.cham1 = form.get("cham1")
            self.wc1 = form.get("wc1")
            self.num1 = form.get("num1")
        if not form.get("num2"):
            self.room2 = form.get("room2")
            self.cham2 = form.get("cham2")
            self.wc2 = form.get("wc2")
            self.num2 = form.get("num2")
    def show(self):
        print("容积率为",end = " ")
        print(self.rate)
        print("建筑密度为", end=" ")
        print(self.density)
        print("限高为", end=" ")
        print(self.limh)
        print("退线距离率为", end=" ")
        print(self.retreat)
        print("出入口方向", end=" ")
        if self.gate_n == "on":
            print("北门",end = " ")
        if self.gate_s == "on":
            print("南门", end=" ")
        if self.gate_w == "on":
            print("西门", end=" ")
        if self.gate_e == "on":
            print("东门", end=" ")
        print("房型1：",end=" ")
        print(str(self.room1)+"室"+str(self.cham1)+"厅"+str(self.wc1)+"卫"+". 房型配比: "+str(self.num1))


class Plan:
    def __init__(self,id,density,sun,low,high):
        self.id = id
        self.density = density
        self.sun = sun
        self.low = low          #低层数
        self.high = high        #高层数

    def listPlan(self):
        l = []
        l.append(self.id)
        l.append(self.density)
        l.append(self.sun)
        l.append(self.low)
        l.append(self.high)
        return l


def typeof(variate):
    type=None
    if isinstance(variate,int):
        type = "int"
    elif isinstance(variate,str):
        type = "str"
    elif isinstance(variate,float):
        type = "float"
    elif isinstance(variate,list):
        type = "list"
    elif isinstance(variate,tuple):
        type = "tuple"
    elif isinstance(variate,dict):
        type = "dict"
    elif isinstance(variate,set):
        type = "set"
    return type
